import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from '../views/Home.vue'
import Classify from '../views/Classify.vue'
import Cart from '../views/Cart.vue'
import Mine from '../views/Mine.vue'
// ---------------------新增引入-------------------------------------
import Goods from '../views/Goods.vue'
import Search from '../views/Search.vue'
import Order from '../views/Order.vue'
import Orderdetails from '../views/Orderdetails'
import Message from '../views/Message.vue'
import Login from '../views/Login.vue'
import Reg from '../views/Reg.vue'
import Ordercart from '../views/Ordercart.vue'
import Address from '../views/AddressEdit.vue'
import Editaddress from '../views/Editaddress.vue'

// 安装插件
Vue.use(VueRouter)

// 实例化router
const router = new VueRouter({
    routes: [{
        path: '/',
        redirect: '/home',
    }, {
        path: '/classify',
        component: Classify,
        name: 'Classify'
    }, {
        path: '/cart',
        component: Cart,
        name: 'Cart'
    }, {
        path: '/mine',
        component: Mine,
        name: 'Mine'
    }, {
        // 我的订单信息
        path: '/order',
        component: Order,
        name: 'Order'
    }, {
        // 我的订单详情页
        path: '/orderdetails',
        component: Orderdetails,
        name: 'Orderdetails'
    }, {
        // 我的信息
        path: '/message',
        component: Message,
        name: 'Message'
    },
    {
        path: '/login',
        component: Login,
        name: 'Login'
    }, {
        path: '/reg',
        component: Reg,
        name: 'Reg'
    }, {
        path: '/ordercart',
        component: Ordercart,
        name: 'Ordercart'
    }, {
        path: '/address',
        component: Address,
        name: 'Address'
    }, {
        path: '/edita',
        component: Editaddress,
        name: 'Editaddress'
    },
    {
        path: "/goods/:id",
        component: Goods,
        name: 'Goods'
    }, {
        path: "/search",
        component: Search,
        name: 'Search'
    },
    {
        path: "/home",
        component: Home,
        name: 'Home'
    },

    ]
})


// // 全局路由守卫
// next 如果有参数就去 参数路由 没有的话就默认想去的地方to
// to 去哪 目标路由
// sfrom 来自哪 当前路由
router.beforeEach((to, from, next) => {
    let token = localStorage.getItem('token');
    if (token && to.path === '/login') {
        next(from)
    } else if (!token && to.path !== '/login') {
        next('/login')
    } else {
        next()
    }
})
export default router;